#ifndef _MULTIPLAYERGAMESERVER_H
#define _MULTIPLAYERGAMESERVER_H

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#pragma once
#include "concurrent\ThreadPool.h"
#include "concurrent\Thread.h"
#include "concurrent\Runnable.h"
#include "concurrent\CriticalSection.h"
#include "net\Winsock2ServerSocket.h"
#include "net\Winsock2SocketFactory.h"
#include "SessionHandler.h"
#include "GameEvent.h"
#include "SceneState.h"
#include "SceneObjectState.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <sstream>
#include <string>
#include <list>

using namespace concurrent;
using namespace std;
using namespace net;

class SessionHandler;

/*
 * This is the server of the tanks game that waits for connections.
 * When a connection comes in, a new game session starts. To handle the session of player,
 * the server creates a SessionHandler, stores it and ask the ThreadPool to run it.
 * The server also holds the name of the scene to load the scene resources.
 */
class MultiplayerGameServer : public Runnable
{
public:
	MultiplayerGameServer(string);
    MultiplayerGameServer(string, string);
    virtual ~MultiplayerGameServer(void);
    int start(void);
	void shutdown(void);
	void update(GameEvent*);
	SceneState* getSceneState();
	void updateSceneState(GameEvent*);
protected:
	virtual DWORD run(void);
private:
	SceneObjectState* createSceneObjectState(GameEvent* gameEvent);
	ServerSocket* socket;
	string port;
	bool isRunning;
	SceneState* sceneState;
	list<SessionHandler*> sessions;
	int sessionId;
	CriticalSection* sessionUpdateLock;
	static string DEFAULT_PORT;
};

#endif
